<?php
class receipts
{
	var $ipsclass;
	var $LIMIT = 10;
	
	function auto_run()
	{
		$redirect = array('list'=>'list_receipts','receipt'=>'view_receipt');
		
		if(!$redirect[$this->ipsclass->input['do']])
		{
			$this->ipsclass->input['do'] = 'list';
		}
		
		$function = $redirect[$this->ipsclass->input['do']];
		$this->$function();
	}
	
	function list_receipts()
	{
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		
		$total = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(DISTINCT time_bought) as total','from'=>'market_buy_logs','where'=>"member_id=".$this->ipsclass->member['id']));
		$page_links = $this->ipsclass->build_pagelinks(	array(	'TOTAL_POSS'   => $total['total'],
																'PER_PAGE'     => $this->LIMIT,
																'CUR_ST_VAL'   => $this->ipsclass->input['st'],
																'L_SINGLE'     => $this->ipsclass->compiled_templates['skin_global']->pagination_compile($this->ipsclass->compiled_templates['skin_global']->pagination_make_jump(),"","",$this->ipsclass->compiled_templates['skin_global']->pagination_current_page(1),"","",1,$this->LIMIT,$this->ipsclass->market_class->base_url."CODE=receipt",0,'st'),
																'BASE_URL'     => $this->ipsclass->market_class->base_url."CODE=receipt",
																'USE_ST'       => 'st',
																'leave_out'    => 5,
														)	);
		
		$receipts = array();
		$this->ipsclass->DB->build_query(array('select'=>'method,time_bought,SUM(quanity) as total_quanity,SUM((price_bought+tax_total)*quanity) as total_price,SUM(resell_price*quanity) as total_resell','from'=>'market_buy_logs','where'=>'member_id='.$this->ipsclass->member['id'],'group'=>'time_bought','order'=>'time_bought DESC','limit'=>array($this->ipsclass->input['st'],$this->LIMIT)));
		$this->ipsclass->DB->exec_query();
		while($receipt = $this->ipsclass->DB->fetch_row())
		{
			$receipt['time'] = $this->ipsclass->get_date($receipt['time_bought'],'LONG');
			$receipt['total_quanity'] = $this->ipsclass->do_number_format($receipt['total_quanity']);
			
			if($receipt['total_price'] <= 0)
			{
				$receipt['total_price'] = $this->ipsclass->lang['free'];
			}
			else
			{
				$receipt['total_price'] = $this->ipsclass->numbers->format_currency($receipt['total_price']);
			}
			if($receipt['total_resell'] <= 0)
			{
				$receipt['total_resell'] = $this->ipsclass->lang['trash'];
			}
			else
			{
				$receipt['total_resell'] = $this->ipsclass->numbers->format_currency($receipt['total_resell']);
			}
			
			$receipts[] = $receipt;
		}
		
		$this->ipsclass->nav[] = "<a href='".$this->ipsclass->market_class->base_url."CODE=receipt'>".$this->ipsclass->lang['receipts']."</a>";
		
		$this->ipsclass->output = $this->ipsclass->compiled_templates['skin_market']->receipt_page($receipts,$page_links);
	}
	
	function view_receipt()
	{
		if(!in_array($this->ipsclass->input['method'],array('resell','buy')))
		{
			$this->ipsclass->boink_it($this->ipsclass->market_class->base_url."CODE=receipt");
		}
		
		$this->ipsclass->input['time'] = intval($this->ipsclass->input['time']);
		
		$total_tax = 0;
		$total_quan = 0;
		$total_price = 0;
		$total_resell = 0;
		$items = array();
		$receipt = array();
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_buy_logs','where'=>"method='".$this->ipsclass->input['method']."' AND time_bought=".$this->ipsclass->input['time']." AND member_id=".$this->ipsclass->member['id']));
		$this->ipsclass->DB->exec_query();
		
		if(!in_array($this->ipsclass->input['method'],array('resell','buy')))
		{
			$this->ipsclass->boink_it($this->ipsclass->market_class->base_url."CODE=receipt");
		}
		
		while($item = $this->ipsclass->DB->fetch_row())
		{
			//$item['item_description'] = $this->ipsclass->parser->pre_display_parse($item['item_description']);
			$item['total_price'] = $this->ipsclass->numbers->multiply($item['quanity'],$item['price_bought']);
			$item['total_resell'] = $this->ipsclass->numbers->multiply($item['quanity'],$item['resell_price']);
			$total_tax = $this->ipsclass->numbers->add($total_tax,$this->ipsclass->numbers->multiply($item['quanity'],$item['tax_total']));
			$total_quan = $this->ipsclass->numbers->add($total_quan,$item['quanity']);
			$total_price = $this->ipsclass->numbers->add($total_price,$item['total_price']);
			$total_resell = $this->ipsclass->numbers->add($total_resell,$item['total_resell']);
			
			if($item['price_bought'] <= 0)
			{
				$item['price_bought'] = $this->ipsclass->lang['free'];
				$item['total_price'] = $this->ipsclass->lang['free'];
			}
			else
			{
				$item['price_bought'] = $this->ipsclass->numbers->format_currency($item['price_bought']);
				$item['total_price'] = $this->ipsclass->numbers->format_currency($item['total_price']);
			}
			if($item['resell_price'] <= 0)
			{
				$item['resell_price'] = $this->ipsclass->lang['trash'];
				$item['total_resell'] = $this->ipsclass->lang['trash'];
			}
			else
			{
				$item['resell_price'] = $this->ipsclass->numbers->format_currency($item['resell_price']);
				$item['total_resell'] = $this->ipsclass->numbers->format_currency($item['total_resell']);
			}
			
			$items[] = $item;
			$receipt = $item;
		}
		
		$receipt['time'] = $this->ipsclass->get_date($receipt['time_bought'],'LONG');
		$receipt['total_tax'] = $total_tax <= 0 ? $this->ipsclass->lang['no_tax'] : $this->ipsclass->numbers->format_currency($total_tax);
		$receipt['total_quanity'] = $this->ipsclass->do_number_format($total_quan);
		$receipt['total_price'] = $total_price <= 0 ? $this->ipsclass->lang['free'] : $this->ipsclass->numbers->format_currency($total_price);
		$receipt['total_resell'] = $total_resell <= 0 ? $this->ipsclass->lang['trash'] : $this->ipsclass->numbers->format_currency($total_resell);
		$receipt['final_price'] = $this->ipsclass->numbers->add($total_price,$total_tax);
		$receipt['final_price'] = $receipt['final_price'] <= 0 ? $this->ipsclass->lang['free'] : $this->ipsclass->numbers->format_currency($receipt['final_price']);
		
		$this->ipsclass->nav[] = "<a href='".$this->ipsclass->market_class->base_url."CODE=receipt'>".$this->ipsclass->lang['receipts']."</a>";
		$this->ipsclass->nav[] = "<a href='".$this->ipsclass->market_class->base_url."CODE=receipt&do=receipt&amp;time=".$this->ipsclass->input['time']."&amp;method=".$this->ipsclass->input['method']."'>".$this->ipsclass->lang['view_receipt']."</a>";
		
		$this->ipsclass->output = $this->ipsclass->compiled_templates['skin_market']->receipt_detail_page($receipt,$items);
	}
}
?>